package net.aiform.record.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.aiform.account.service.PersonService;
import net.aiform.bean.Constant;
import net.aiform.bean.JSONMessage;
import net.aiform.record.pojo.Record;
import net.aiform.record.pojo.Value;
import net.aiform.record.service.RecordService;
import net.aiform.template.model.TemplateRecords;
import net.aiform.template.pojo.Template;
import net.aiform.template.service.TemplateService;

import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.ServletRequestAware;

import com.googlecode.jsonplugin.annotations.JSON;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.Preparable;
import com.utils.DateUtils;

import ext.cjt.CommonMethods;

public class CreateRecordAction implements Preparable, ServletRequestAware {
	private Logger log = Logger.getLogger(this.getClass());
	private JSONMessage json;
	private PersonService personService;
	private TemplateService templateService;
	private RecordService recordService;
	private HttpServletRequest req;
	private String code;
	private List<Map<String, String>> params;
	private String arg1;

	public String fetchNewRecord() {
		String name = CommonMethods.getUrlLastPart(this.req.getServletPath());
		Template t = this.templateService.get(name);
		Record r = this.recordService.getNewRecord(t);
		//this.recordService.save(r);
		r.setCode(r.getId() + "");
		Map<String, Object> value = new HashMap<String, Object>();
		List<Record> rs = new ArrayList<Record>();
		rs.add(r);
		TemplateRecords tr = new TemplateRecords();
		tr.setTemplate(t);
		tr.setRecords(rs);
		value.put("value", tr);
		json.setData(value);
		return Action.SUCCESS;
	}

	public String beginToCreate() {
		return Action.SUCCESS;
	}

	@SuppressWarnings("unchecked")
	public String saveRecord() {
		params = (List<Map<String, String>>) req.getAttribute(Constant.PARAMS);
		for (Iterator<Map<String, String>> iter = params.iterator(); iter.hasNext();) {
			Map<String, String> map = (Map<String, String>) iter.next();
			String id = map.get(Constant.FORM_RECORD_ID);
			Record r = this.recordService.get(Integer.parseInt(id));
			List<Value> values = r.getValues();
			for (Iterator<Value> iter2 = values.iterator(); iter2.hasNext();) {
				Value v = (Value) iter2.next();
				assignValue(v, map);
			}
			this.recordService.save(r);
		}
		return Action.SUCCESS;
	}

	private void assignValue(Value v, Map<String, String> map) {
		String type = v.getDataType();
		if (Constant.TYPE_STRING.equalsIgnoreCase(type)) v.setS(map.get(v.getCode()));
		if (Constant.TYPE_DATE.equalsIgnoreCase(type)) v.setD(DateUtils.toDate(map.get(v.getCode())));
		if (Constant.TYPE_FLOAT.equalsIgnoreCase(type)) v.setF(Float.valueOf(map.get(v.getCode())));
		if (Constant.TYPE_INT.equalsIgnoreCase(type)) v.setI(Integer.valueOf(map.get(v.getCode())));
	}

	public String execute() {
		return Action.SUCCESS;
	}

	public CreateRecordAction() {
		super();
		this.json = new JSONMessage();
	}

	public CreateRecordAction(PersonService personService, TemplateService templateService, RecordService recordService) {
		super();
		this.personService = personService;
		this.templateService = templateService;
		this.recordService = recordService;
		this.json = new JSONMessage();
	}

	public void prepare() throws Exception {

	}

	@JSON(serialize = false)
	public String getArg1() {
		return arg1;
	}

	public void setArg1(String arg1) {
		this.arg1 = arg1;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.req = request;
	}

	@JSON(serialize = false)
	public TemplateService getTemplateService() {
		return templateService;
	}

	public void setTemplateService(TemplateService templateService) {
		this.templateService = templateService;
	}

	@JSON(serialize = false)
	public PersonService getPersonService() {
		return personService;
	}

	public void setPersonService(PersonService personService) {
		this.personService = personService;
	}

	@JSON(serialize = false)
	public Logger getLog() {
		return log;
	}

	public void setLog(Logger log) {
		this.log = log;
	}

	@JSON(serialize = false)
	public HttpServletRequest getReq() {
		return req;
	}

	public void setReq(HttpServletRequest req) {
		this.req = req;
	}

	public JSONMessage getJson() {
		return json;
	}

	public void setJson(JSONMessage json) {
		this.json = json;
	}

	@JSON(serialize = false)
	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	@JSON(serialize = false)
	public List<Map<String, String>> getParams() {
		return params;
	}

	public void setParams(List<Map<String, String>> params) {
		this.params = params;
	}

	@JSON(serialize = false)
	public RecordService getRecordService() {
		return recordService;
	}

	public void setRecordService(RecordService recordService) {
		this.recordService = recordService;
	}
}
